//
//  ChangePWDViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/6/13.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class ChangePWDViewController: BaseViewController {

    var label,label2: UILabel!
    var passwordField,passwordField2: FUITextField!
    var button: FUIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.title = "修改密码"
        
        InitViews()
    }
    
    func InitViews() {
        
        label = UILabel.init(frame: CGRect(x: 20, y: 84, width: screenW - 40, height: 30))
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = lightText
        label.text = "设置密码"
        
        passwordField = FUITextField.init(frame: CGRect(x: 20, y: label.frame.maxY, width: screenW - 40, height: 40))
        passwordField.backgroundColor = UIColor.clear
        passwordField.layer.cornerRadius = 3.0
        passwordField.layer.borderColor = lightText.cgColor
        passwordField.layer.borderWidth = 1.0
        passwordField.cornerRadius = 3.0
        passwordField.borderColor = barColor
        passwordField.borderWidth = 2.0
        passwordField.textFieldColor = UIColor.white
        passwordField.font = UIFont.systemFont(ofSize: 14)
        passwordField.edgeInsets = UIEdgeInsetsMake(0, 8, 0, 8)
        passwordField.placeholder = "输入6位以上密码"
        passwordField.isSecureTextEntry = true
        
        label2 = UILabel.init(frame: CGRect(x: 20, y: passwordField.frame.maxY, width: screenW - 40, height: 30))
        label2.font = UIFont.systemFont(ofSize: 14)
        label2.textColor = lightText
        label2.text = "重复密码"
        
        passwordField2 = FUITextField.init(frame: CGRect(x: 20, y: label2.frame.maxY, width: screenW - 40, height: 40));
        passwordField2.backgroundColor = UIColor.clear
        passwordField2.layer.cornerRadius = 3.0
        passwordField2.layer.borderWidth = 1.0
        passwordField2.layer.borderColor = lightText.cgColor
        passwordField2.cornerRadius = 3.0
        passwordField2.borderColor = barColor
        passwordField2.borderWidth = 2.0
        passwordField2.textFieldColor = UIColor.white
        passwordField2.font = UIFont.systemFont(ofSize: 14)
        passwordField2.edgeInsets = UIEdgeInsetsMake(0, 8, 0, 8)
        passwordField2.placeholder = "请再次输入密码"
        passwordField2.isSecureTextEntry = true
        
        button = FUIButton.init(frame: CGRect(x: 20, y: passwordField2.frame.maxY + 10, width: screenW - 40, height: 50))
        button.buttonColor = barColor
        button.shadowColor = barColor
        button.cornerRadius = 6
        button.shadowHeight = 3
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.setTitle("确  定", for: UIControlState())
        button.setTitleColor(UIColor.white, for: UIControlState())
        button.setTitleColor(UIColor.white, for: UIControlState.highlighted)
        button.addTarget(self, action: #selector(ChangePWDViewController.buttonClick), for: UIControlEvents.touchDown)
        
        self.view.addSubview(label)
        self.view.addSubview(passwordField)
        self.view.addSubview(label2)
        self.view.addSubview(passwordField2)
        self.view.addSubview(button)
        
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        passwordField.resignFirstResponder()
        passwordField2.resignFirstResponder()
    }
    func buttonClick() {
        let password = passwordField.text!
        let password2 = passwordField2.text!
        if password.characters.count >= 6 {
            if password == password2 {
                changePassword(password)
            }else{
                let cancelAction = UIAlertAction.init(title: "知道了", style: UIAlertActionStyle.cancel, handler: nil)
                let alert: UIAlertController = UIAlertController.init(title: "提示", message: "对不起，您输入的密码不一致，请重新输入！", preferredStyle: UIAlertControllerStyle.alert)
                alert.addAction(cancelAction)
                self.present(alert, animated: true, completion: nil)
                
            }
        }else{
            let cancelAction = UIAlertAction.init(title: "知道了", style: UIAlertActionStyle.cancel, handler: nil)
            let alert: UIAlertController = UIAlertController.init(title: "提示", message: "您输入的密码过于简单，请输入6位以上的密码！", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(cancelAction)
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    func changePassword(_ password: String!) {
        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        hud?.animationType = MBProgressHUDAnimation.fade
        hud?.labelText = "请稍候..."
        let user = getUserPojo()

        let url = "\(server)\(adminwebapppi)"
        let request = "<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\"><channel><t>35</t><uid>\(user.userid!)</uid><sch>\(user.schid!)</sch><pwd>\(password!)</pwd></channel></rss>"
        HttpPostURL(url, requestXML: request) { (data, result, error) in
            if error == nil {
                if result == "True" {
                    let defaults = UserDefaults.standard
                    defaults.set(password, forKey: "password")
                    defaults.synchronize()
                    UIAlertView.init(title: "提示", message: "密码修改成功，下次请使用新密码登录！", delegate: nil, cancelButtonTitle: "知道了").show()
                    let index = self.navigationController?.viewControllers.index(of: self)
                    self.navigationController?.popToViewController((self.navigationController?.viewControllers[index! - 1])!, animated: true)
                    self.dismiss(animated: true, completion: nil)
                    
                }else{
                    let cancelAction = UIAlertAction.init(title: "知道了", style: UIAlertActionStyle.cancel, handler: nil)
                    let alert: UIAlertController = UIAlertController.init(title: "提示", message: "修改密码失败，请稍后再试！", preferredStyle: UIAlertControllerStyle.alert)
                    alert.addAction(cancelAction)
                    self.present(alert, animated: true, completion: nil)
                }
                
                hud?.hide(true)
            }else{
                hud?.hide(true)
                UIAlertView.init(title: "错误提示", message: error?.localizedDescription, delegate: nil, cancelButtonTitle: "知道了").show()
            }
        }
        
    }


}
